<?php

use yii\helpers\Url;
use common\models\Article;
$this->addCssFile([
    "plugins/webuploader/webuploader.css"
]);

$this->addJsFile([
    "plugins/webuploader/webuploader.min.js",
    "js/webupload.js"
]);
$this->inline("title","添加服务");
?>
<div class="panel">
    <div class="panel-body">
        <div class="row">
            <div class="col-sm-12">
                <div class="card-box">
                    <h4 class="m-t-0 m-b-30 header-title"><b>添加服务</b></h4>
                    <p class="text-muted font-13 m-b-30">
                        服务中心展示内容
                    </p>
                    <form id="feature" onsubmit="return false" enctype="multipart/form-data">
                        <input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->csrfToken?>">
                        <input type="hidden" name="id" value="<?=isset($detail['id']) ? $detail['id'] : 0;?>">
                        <div class="form-group">
                            <label>服务名称</label>
                            <input class="form-control" name="title" placeholder="企业网站..." value="<?=isset($detail['title']) ? $detail['title'] : ''?>"  >
                        </div>

                        <div class="form-group">
                            <label>服务描述</label>
                            <textarea class="form-control" name="desc" placeholder="中小企业基础功能网站"><?php echo isset($detail['desc']) ? $detail['desc']:"";?></textarea>
                        </div>
                        <div class="form-group">
                            <label>上传缩略图：</label>
                            <div id="fileSelect" class="waves-effect waves-light">选择图片</div>
                            <input type="hidden" name="picture" value="<?=isset($detail['picture']) ? $detail['picture'] : ''?>">
                        </div>
                        <div class="form-group">
                            <img style="width: 300px;height: 150px;border: 1px dashed rgba(0, 0, 0, 0.1);padding: 5px;" src="<?php echo isset($detail['picture']) ? $this->imghost.$detail['picture']:''?>" class="img-responsive uploadImg">
                        </div>

                        <div class="checkbox checkbox-primary checkbox-inline">
                            <input <?php echo isset($detail['status']) && $detail['status'] == 0?"checked":"";?> type="checkbox" value="0" name="status"><label>提交审核</label>
                        </div>
                        <div class="checkbox checkbox-primary checkbox-inline">
                            <input <?php echo isset($detail['status']) && $detail['status'] == 1?"checked":"";?> type="checkbox" value="1" name="status"><label>申请置顶</label>
                        </div>
                        <div class="checkbox checkbox-primary checkbox-inline">
                            <input <?php echo isset($detail['status']) && $detail['status'] == 2?"checked":"";?> type="checkbox" value="2" name="status"><label>立即发布</label>
                        </div>
                        <div class="form-group text-right m-b-0">
                            <button class="btn btn-primary waves-effect waves-light" onclick="saveObj.save()" type="button">
                                提 交
                            </button>
                            <button type="reset" class="btn btn-default waves-effect waves-light m-l-5">
                                重 置
                            </button>
                            <button onclick="location.href='<?=Url::toRoute('/feature')?>'" type="button" class="btn btn-default waves-effect waves-light m-l-5">
                                返 回
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php $this->beginBlock("jsText","append");?>
<script type="text/javascript">
    var formData = {};
    var csrfToken = $('meta[name="csrf-token"]').attr("content");
    var csrfParam = $('meta[name="csrf-param"]').attr("content");

    // 请求数据
    formData[csrfParam] = csrfToken;
    formData['category'] = 'image';
    formData['action'] = 'feature';

    // 上传插件初始化
    var option = {
        pick: {id: '#fileSelect', multiple: false},
        formData: formData, //category:image 图片 document 文档  media 媒体
        server:'<?php echo Yii::$app->urlManager->createUrl(['api/upload/index'])?>',
        fileSingleSizeLimit: 10 * 1024 * 1024,
        accept: {
            title: 'Image',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'Image/gif,Image/jpeg,Image/bmp,Image/png'
        },
        func: {
            uploadSuccess: function (file, args)
            {
                var url = '<?php echo $this->imghost?>'+args.path;
                if (args.state === 'SUCCESS') {
                    $(".uploadImg").attr('src',url);
                    $("input[name=picture]").val(args.path);
                } else {
                    console.log(file);
                }
            }
        }
    };
    uploadObject.init(option);

    var saveObj = {
        ajaxLock:false,
        save:function () {
            that = this;
            var data = $("#feature").serialize();
            var submitUrl = "<?php echo Yii::$app->urlManager->createUrl(['feature/save'])?>";

            if (that.ajaxLock)return false;
            that.ajaxLock = true;

            $.ajax({
                data:data,
                url:submitUrl,
                type:'post',
                dataType:'json',
                success:function (resp) {
                    $.Notification.autoHideNotify(resp.state, 'bottom right', '提示信息',resp.message);
                    that.ajaxLock = false;
                },
                error:function () {
                    that.ajaxLock = false;
                }
            });
        }
    };
</script>
<?php $this->endBlock();?>
